<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.businessId? $t('add') : $t('update')"
             :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
             :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item label="商家名" prop="businessName">
        <el-input v-model="dataForm.businessName" placeholder="商家名"></el-input>
      </el-form-item>
      <el-form-item label="商家地址" prop="businessAddress">
        <el-input v-model="dataForm.businessAddress" placeholder="商家地址"></el-input>
      </el-form-item>
      <el-form-item label="商家电话" prop="businessTel">
        <el-input v-model="dataForm.businessTel" placeholder="商家电话"></el-input>
      </el-form-item>
      <el-form-item label="商家图片" prop="businessPic">
        <el-upload
            class="upload-demo"
            action="/file/upload"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-success="handleSuccess"
            :file-list="fileList"
            list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        businessId: '',
        businessName: '',
        businessAddress: '',
        businessTel: '',
        businessPic: ''
      },
      fileList: [],
    }
  },
  computed: {
    dataRule() {
      return {
        businessName: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        businessAddress: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        businessTel: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.businessId) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/business/business/${this.dataForm.businessId}`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.businessId ? 'post' : 'put']('/business/business/', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          location.reload()
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {
        })
      })
    }, 1000, {'leading': true, 'trailing': false}),
    handleRemove(file) {
      this.$http
          .delete("/file", {
            params: {
              fileInfo: file.response.data,
            },
          })
          .then(({data: res}) => {
            this.$message({
              message: `${res.data}`,
              type: "success",
            });
          });
    },
    // 点击图片
    handlePreview(file) {
      console.log(file);
    },
    // 上传图片成功后
    handleSuccess(response) {
      console.log(response)
      this.dataForm.businessPic = response.data
    },
  }
}
</script>
